/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package com.l2jfrozen.gameserver.model.actor.instance;

import java.util.Map;

import com.l2jfrozen.Config;
import com.l2jfrozen.gameserver.ai.CtrlIntention;
import com.l2jfrozen.gameserver.ai.L2AttackableAI;
import com.l2jfrozen.gameserver.ai.L2CharacterAI;
import com.l2jfrozen.gameserver.ai.L2NpcWalkerAI;
import com.l2jfrozen.gameserver.model.L2Attackable;
import com.l2jfrozen.gameserver.model.L2Character;
import com.l2jfrozen.gameserver.model.L2Summon;
import com.l2jfrozen.gameserver.model.actor.instance.L2PetInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2SummonInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2NpcInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2MonsterInstance;
import com.l2jfrozen.gameserver.network.serverpackets.ActionFailed;
import com.l2jfrozen.gameserver.network.serverpackets.MyTargetSelected;
import com.l2jfrozen.gameserver.network.serverpackets.CreatureSay;
import com.l2jfrozen.gameserver.templates.L2NpcTemplate;

/**
 * This class manages some npcs can walk in the city. <br>
 * It inherits all methods from L2NpcInstance. <br>
 * <br>
 * 
 * @original author Rayan RPG
 * @since 819
 */

public class L2NpcWalkerInstance extends L2Attackable
//extends L2NpcInstance
{
	/**
	 * Constructor of L2NpcWalkerInstance (use L2Character and L2NpcInstance constructor).
	 */
	
	public L2NpcWalkerInstance(int objectId, L2NpcTemplate template)
	{
		super(objectId, template);
		setAI(new L2NpcWalkerAI(new L2NpcWalkerAIAccessor()));
	}
	
	/**
	 * AI can't be deattached, npc must move always with the same AI instance.
	 * 
	 * @param newAI AI to set for this L2NpcWalkerInstance
	 */
	
	@Override
	public void setAI(L2CharacterAI newAI)
	{
		if(_ai == null)
		{
			super.setAI(newAI);
		}
	}
	
	@Override
	public void onSpawn()
	{
		((L2NpcWalkerAI) getAI()).setHomeX(getX());
		((L2NpcWalkerAI) getAI()).setHomeY(getY());
		((L2NpcWalkerAI) getAI()).setHomeZ(getZ());
	}
	
	/**
	 * Sends a chat to all _knowObjects
	 * 
	 * @param chat message to say
	 */
	
	public void broadcastChat(String chat)
	{
		Map<Integer, L2PcInstance> _knownPlayers = getKnownList().getKnownPlayers();

		if(_knownPlayers == null)
		{
			if(Config.DEVELOPER)
			{
				_log.info("broadcastChat _players == null");
			}
			return;
		}

		//We send message to known players only!
		if(_knownPlayers.size() > 0)
		{
			CreatureSay cs = new CreatureSay(getObjectId(), 0, getName(), chat);
			
			//We interact and list players here
			for(L2PcInstance players : _knownPlayers.values())
			{
				//Finally send packet
				players.sendPacket(cs);
			}
			cs = null;
		}
	}
	
	/**
	 * NPCs are immortal
	 * 
	 * @param killer ignore it
	 * @return false
	 */
	
	@Override
	public boolean doDie(L2Character killer)
	{
		//Kill the L2PcInstance
		if(!super.doDie(killer))
			return false;
		
		if(killer != null)
		{
			L2PcInstance pk = null;
			
			if(killer instanceof L2PcInstance)
			{
				pk = (L2PcInstance) killer;
			}
			
			if(killer instanceof L2Summon)
			{
				pk = ((L2Summon) killer).getOwner();
			}
			
			if(pk != null)
			{
				pk.setPkKills(pk.getPkKills() + 1);
				pk.setKarma(pk.getKarma() + 1200);
			}
			pk = null;
		}
		return true;
	}
	
	
	@Override
	public L2CharacterAI getAI()
	{
		return super.getAI();
	}
	
	protected class L2NpcWalkerAIAccessor extends L2Character.AIAccessor
	{
		//AI can't be deattached.
		@Override
		public void detachAI(){}
	}
	
	public void onAction(L2PcInstance player)
	{
		if(getObjectId() != player.getTargetId())
		{
			player.setTarget(this);
			MyTargetSelected my = new MyTargetSelected(getObjectId(), 0);
			player.sendPacket(my);
		}
		else
		{
			player.sendPacket(ActionFailed.STATIC_PACKET);
		}
	}
	
	@Override
	public boolean isAutoAttackable(L2Character attacker)
	{
		if(attacker instanceof L2PcInstance)
		{
			//return ((L2PcInstance) attacker).getKarma() > 0;
			return attacker instanceof L2PcInstance;
		}
		
		// Return True if the attacker is a L2MonsterInstance.
		if(attacker instanceof L2MonsterInstance)
		{
			return attacker instanceof L2MonsterInstance;
		}
		
		return false;
	}
	
	@Override
	public boolean isAggressive()
	{
		return true;
	}
	
	/*@Override
	public void onSpawn()
	{
		super.onSpawn();
		_homeX = getX();
		_homeY = getY();
		_homeZ = getZ();
		
		// check the region where this mob is, do not activate the AI if region is inactive.
		L2WorldRegion region = L2World.getInstance().getRegion(getX(), getY());
		if(region != null && !region.isActive())
		{
			((L2AttackableAI) getAI()).stopAITask();
		}
		region = null;
	}*/
}